<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div id="app">
    <!--搜索-->
    <el-row>
        <el-col :span="4">
            <el-input v-model="searchForm.categoryName" placeholder="请输入分类名" size="mini"></el-input>
        </el-col>
        <el-col :span="3">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="search">搜索</el-button>
        </el-col>
        <el-col :span="3">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">刷新</el-button>
        </el-col>
        <el-col :span="4">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="add">新增</el-button>
        </el-col>
        <el-col :span="10"></el-col>
    </el-row>

    <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
            border
            @selection-change="">
        <el-table-column
                label="多选"
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="categoryId"
                label="编号"
                width="80">
        </el-table-column>
        <el-table-column
                prop="categoryName"
                label="图书分类"
                width="120">
        </el-table-column>
        <el-table-column
                prop=""
                label="操作"
                width="">
            <template slot-scope="scope">
                <el-button type="primary" @click="update(scope.$index, scope.row)">修改</el-button>
                <el-button type="danger" @click="deleteById(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=currentPage
            :page-sizes="[5, 10, 15, 20]"
            :page-size=pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total=total>
    </el-pagination>
    <!--修改-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">

        <el-form :model="form" :rules="rules" ref="form">
            <el-form-item label="分类名" prop="categoryName" :label-width="formLabelWidth">
                <el-input v-model="form.categoryName" size="mini"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
    </el-dialog>


</div>
</body>
<!--引入vue-->
<script src="/js/vue.js"></script>
<!-- 引入组件element-ui库 -->
<script src="/js/index.js"></script>
<!--引入axios库-->
<script src="/js/axios.min.js"></script>
<script type="text/javascript">
    var app = new Vue({
        el: "#app",
        data: function () {
            return {
                currentPage: 1,
                pageSize: 5,
                total: 0,
                tableData: [],
                searchForm: {},
                form: {},
                title: "",
                dialogFormVisible: false,
                cateDate: [],
                checked: false,
                formLabelWidth: '120px',
                rules: {
                    categoryName: [
                        {required: true, message: '请输入员工姓名', trigger: 'blur'}
                    ]
                }
            }
        },
        created: function () { //初始化方法 类似于jquery  $({})
            this.loadData();
        },
        methods: {
            loadData() {
                //让_this等于当前vue对象
                var _this = this;
                //赋值 axios官网代码
                axios.post('/cate/list', {
                    pageNum: this.currentPage,
                    pageSize: this.pageSize,
                    data: this.searchForm
                })  //指定请求的url及参数  get
                    .then(function (response) {  //正确返回对象的处理
                        // handle success
                        //JSON.stringify   把json转换为json字符串，方便查看
                        console.log(JSON.stringify(response));
                        //直接使用this 会指 axios对象  错误
                        // this.tableData
                        //判断返回值是否正确
                        if (response.data.code == 200) {
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;

                        }

                    })
                    .catch(function (error) {//错误的处理
                        // handle error
                        console.log(error);
                    })
                    .then(function () {//总执行 最终处理      类似java  异常处理的finally
                        // always executed
                    });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.pageSize = val;
                //再次请求，加载数据
                this.loadData();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                //再次请求，加载数据
                this.loadData();
            },
            search() {
                //直接调用重新请求，加载数据方法
                this.loadData();
            },
            add(){
                //显示对话框
                this.dialogFormVisible=true;
                //指定对话框标题
                this.title = '添加分类';
                //让form表单重新赋空值
                this.form={};
            },
            update(index, row){
                //row  当前行数据 如果是第一行  row = {deptId:1,deptName:'管理部',description:'店长的事',status:1}
                console.log(index, row);
                //显示对话框
                this.dialogFormVisible=true;
                //指定对话框标题
                this.title = '修改分类';
                //把一行数据赋值给form赋值
                this.form = row;
                this.form.categoryId = row.categoryId;
                this.form.categoryName = row.categoryName;

            },
            submitForm(formName) {

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var  url  = "";
                        if(this.title=='添加分类'){
                            url='/cate/insert';
                        }else if(this.title=='修改分类'){
                            url='/cate/update';
                        }
                        //让_this等于当前vue对象
                        var _this = this;
                        // alert(111);
                        //赋值 axios官网代码  this.form =form:{deptName:'',description:'',status:1}
                        axios.post(url,this.form)  //指定请求的url及参数  get
                            .then(function (response) {  //正确返回对象的处理
                                // handle success
                                console.log(response);
                                //JSON.stringify   把json转换为json字符串，方便查看
                                console.log(JSON.stringify(response));
                                //直接使用this 会指 axios对象  错误
                                // this.tableData
                                //判断返回值是否正确
                                if(response.data.code==200){

                                    //关闭弹框
                                    _this.dialogFormVisible=false;
                                    //提示添加成功
                                    _this.$message({
                                        message: '恭喜你，这是一条成功消息',
                                        type: 'success'
                                    });
                                    //重新加载数据
                                    _this.loadData();
                                }else {
                                    //错误提示信息
                                    _this.$message.error('错了哦，这是一条错误消息');
                                }

                            })
                            .catch(function (error) {//错误的处理
                                // handle error
                                console.log(error);
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            deleteById(index, row) {

                //确认框提示
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    //让_this等于当前vue对象
                    var _this = this;
                    //alert(111);
                    //赋值 axios官网代码  this.form =form:{deptName:'',description:'',status:1}
                    axios.delete("/cate/deleteById?categoryId="+row.categoryId)  //指定请求的url及参数  get
                        .then(function (response) {  //正确返回对象的处理
                            // handle success

                            //JSON.stringify   把json转换为json字符串，方便查看
                            console.log(JSON.stringify(response));
                            //直接使用this 会指 axios对象  错误
                            // this.tableData
                            //判断返回值是否正确
                            if(response.data.code==200){
                                //提示添加成功
                                _this.$message({
                                    message: '恭喜你，这是一条成功消息',
                                    type: 'success'
                                });
                                //重新加载数据
                                _this.loadData();
                            }else {
                                //错误提示信息
                                _this.$message.error('错了哦，这是一条错误消息');
                            }

                        })
                        .catch(function (error) {//错误的处理
                            // handle error
                            console.log(error);
                        })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>